public class Alu {

	int Operand1, Operand2;
	int control;
	int output;

	// Function ALU control
	// AND  0 F = A & B (bitwise AND)
	// OR   1 F = A | B (bitwise OR);
	// add  2 F = A + B; Z,V,C update
	// sltu 3 F = (abs(A) < abs(B))? 1 : 0 
	// sll  4 F = A << B  
	// srl  5 F = A >> B
	// sub  6 F = A - B; Z,V,C update
	// slt  7 F = (A < B) ? 1 : 0
	// NOR 12 F = ~ ( A | B ) (bitwise NOR)
	public Alu()
	{
		output = 0;
		Operand1=0;
	    Operand2 = 0;
	    output = 0 ;
	}

	public int Perform(int op1, int op2, int control) {
		this.Operand1 = op1;
		this.Operand2 = op2;
		this.control = control;

		if (this.control == 0)
			this.output = this.Operand1 & this.Operand2;
		
		else if (this.control == 1)
			this.output = this.Operand1 | this.Operand2;
		
		else if (this.control == 2)
			this.output = this.Operand1 + this.Operand2;
		
		else if (this.control == 3)
			this.output = (Math.abs(this.Operand1) < Math.abs(this.Operand2))? 1 : 0;
		
		else if (this.control == 4)
			this.output = this.Operand1 << this.Operand2;
		
		else if (this.control == 5)
			this.output = this.Operand1 >> this.Operand2;
			
		else if (this.control == 6)
			this.output = this.Operand1 - this.Operand2;
		
		else if (this.control == 7)
			this.output = (this.Operand1 < this.Operand2)? 1 : 0;
		
		else if (this.control == 12)
			this.output = ~(this.Operand1 | this.Operand2);
		
		//System.out.println(output);
	return output;
	}
	
}